package com.tubruk.trackme;

import java.util.ArrayList;

import com.tubruk.adapter.GridMenuAdapter;
import com.tubruk.database.DatabaseHelper;
import com.tubruk.object.Global;
import com.tubruk.object.GridMenu;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;

public class StartMenu extends Activity{
	GridView grid_main;

	private ArrayList<GridMenu> menus;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.startmenu);
		
		//hahahahahha
		Global.activityContext = this;
		
		//to initialize constant values
		DatabaseHelper db = new DatabaseHelper(getBaseContext());
    	db.getWritableDatabase();
    	db.setupInitialValues();
    	db.destroy();
		
		menus = new ArrayList<GridMenu>();

		menus.add(new GridMenu(R.drawable.playback, "History"));
		menus.add(new GridMenu(R.drawable.recording2, "Import"));
		menus.add(new GridMenu(R.drawable.import2, "Settings"));
		menus.add(new GridMenu(R.drawable.recording2, "Exit"));        

		grid_main = (GridView)findViewById(R.id.GridView01);
		grid_main.setAdapter(new GridMenuAdapter(this, getLayoutInflater(), menus));	


		grid_main.setOnItemClickListener(new GridItemClickListener());
		
		ImageButton locate = (ImageButton) findViewById(R.id.locate);
		locate.setOnClickListener(new ActionClickListener(StartMenu.this, TrackMapActivity.class));
		
		ImageButton search = (ImageButton) findViewById(R.id.search);
		search.setOnClickListener(new ActionClickListener(StartMenu.this, History.class));
	}
	
	private class ActionClickListener implements OnClickListener {
		private Class target;
		private Context source;
		
		public ActionClickListener(Context source, Class target){
			this.source = source;
			this.target = target;
		}
		
		public void onClick(View v) {
			Intent i = new Intent(source, target);
			startActivity(i);
		}
		
	}

	private class GridItemClickListener implements AdapterView.OnItemClickListener {
		public void onItemClick(AdapterView parent, 
				View v, int position, long id) 
		{
			Intent i;
			
			switch(position){
			case 0:
				i = new Intent(StartMenu.this, History.class);
				startActivity(i);
				break;
			case 1:
				i = new Intent(StartMenu.this, ImportData.class);
				startActivity(i);
				break;
			case 2:
				i = new Intent(StartMenu.this, Options.class);
				startActivity(i);
				break;
			case 3:
				finish();
				break;
		
			
			}
		}
	}
}
